<?php

namespace maotool\util;

use maotool\file\DirUtil;
use maotool\file\FileUtil;
use maotool\log\Console;
use maotool\object\ReflectionUtil;
use maotool\object\StrUtil;

/**
 * 图片工具类
 */
class ImageUtil
{

    /**
     * 是否是图片
     * @param $path
     * @param $config
     * @return bool
     */
    public static function isImage($path, $config = [])
    {
        $path = PathUtil::path($path);
        if (FileUtil::notExist($path)) {
            return false;
        }
        $imageInfo = @getimagesize($path);
        if ($imageInfo === false) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 图片压缩
     * @param $path
     * @param $quality
     * @param $config
     * @return void
     */
    public static function compress($path, $quality = 100, $config = [])
    {
        if (class_exists('Imagick')) {
            $files = DirUtil::scanFiles($path);
            foreach ($files as $file) {
                if (self::isImage($file['path'])) {
                    $imagick = ReflectionUtil::instance("Imagick", [$file['path']]);
                    $imagick->setImageCompressionQuality($quality);
                    $imagick->writeImage();
                    $imagick->destroy();
                }
            }
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 图片质量
     * @param $image
     * @param $config
     * @return int
     */
    public static function quality($image, $config = [])
    {
        if (class_exists('Imagick')) {
            $imagick = ReflectionUtil::instance("Imagick", [$image]);
            $quality = $imagick->getImageCompressionQuality();
            $imagick->destroy();
            return $quality;
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 调整图像大小
     * @param $path
     * @param $width
     * @param $height
     * @param $config
     * @return void
     */
    public static function resize($path, $width = null, $height = null, $config = [])
    {
        if (class_exists('Imagick')) {
            $files = DirUtil::scanFiles($path);
            foreach ($files as $file) {
                if (self::isImage($file['path'])) {
                    $imagick = ReflectionUtil::instance("Imagick", [$file['path']]);
                    $originalWidth = $imagick->getImageWidth();
                    $originalHeight = $imagick->getImageHeight();
                    if (null === $width && null === $height) {
                        return;
                    }
                    if (StrUtil::endsWith($width, "%")) {
                        $newWidth = $originalWidth * (int)substr($width, 0, -1) / 100;
                    } else if (StrUtil::endsWith($width, "min")) {
                        $minWidth = (int)substr($width, 0, -3);
                        $newWidth = max($originalWidth, $minWidth);
                    } else if (StrUtil::endsWith($width, "max")) {
                        $maxWidth = (int)substr($width, 0, -3);
                        $newWidth = min($originalWidth, $maxWidth);
                    } else {
                        $newWidth = $width;
                    }
                    if (StrUtil::endsWith($height, "%")) {
                        $newHeight = $originalHeight * (int)substr($height, 0, -1) / 100;
                    } else if (StrUtil::endsWith($height, "min")) {
                        $minHeight = (int)substr($height, 0, -3);
                        $newHeight = max($originalHeight, $minHeight);
                    } else if (StrUtil::endsWith($height, "max")) {
                        $maxHeight = (int)substr($height, 0, -3);
                        $newHeight = min($originalHeight, $maxHeight);
                    } else {
                        $newHeight = $height;
                    }
                    if (is_null($newWidth)) {
                        $ratio = $newHeight / $originalHeight;
                        $newWidth = (int)($originalWidth * $ratio);
                    } else if (is_null($newHeight)) {
                        $ratio = $newWidth / $originalWidth;
                        $newHeight = (int)($originalHeight * $ratio);
                    }
                    $imagick->resizeImage($newWidth, $newHeight, ReflectionUtil::getConstant("Imagick", 'FILTER_LANCZOS'), 1, false);
                    $imagick->writeImage();
                    $imagick->destroy();
                }
            }
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 横屏
     * @param $path
     * @param $config
     * @return bool
     */
    public static function isHorizontal($path, $config = [])
    {
        if (class_exists('Imagick')) {
            $path = PathUtil::path($path);
            $imagick = ReflectionUtil::instance("Imagick", [$path]);
            return $imagick->getImageWidth() >= $imagick->getImageHeight();
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 竖屏
     * @param $path
     * @param $config
     * @return bool
     */
    public static function isVertical($path, $config = [])
    {
        if (class_exists('Imagick')) {
            $path = PathUtil::path($path);
            $imagick = ReflectionUtil::instance("Imagick", [$path]);
            return $imagick->getImageWidth() <= $imagick->getImageHeight();
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 长方形
     * @param $path
     * @param $config
     * @return bool
     */
    public static function isRectangle($path, $config = [])
    {
        if (class_exists('Imagick')) {
            $path = PathUtil::path($path);
            $imagick = ReflectionUtil::instance("Imagick", [$path]);
            return $imagick->getImageWidth() != $imagick->getImageHeight();
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 正方形
     * @param $path
     * @param $config
     * @return bool
     */
    public static function isSquare($path, $config = [])
    {
        if (class_exists('Imagick')) {
            $path = PathUtil::path($path);
            $imagick = ReflectionUtil::instance("Imagick", [$path]);
            return $imagick->getImageWidth() == $imagick->getImageHeight();
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

    /**
     * 是否是窄图
     * @param $path
     * @param $multiply
     * @param $config
     * @return bool
     */
    public static function isNarrow($path, $multiply = 3, $config = [])
    {
        if (class_exists('Imagick')) {
            $path = PathUtil::path($path);
            $imagick = ReflectionUtil::instance("Imagick", [$path]);
            if (($imagick->getImageWidth() / $imagick->getImageHeight() >= $multiply)) {
                return true;
            } else if ($imagick->getImageHeight() / $imagick->getImageWidth() >= $multiply) {
                return true;
            }
            return false;
        } else {
            throw new \RuntimeException("Imagick extension is not installed.");
        }
    }

}